/*
 * StatusView.java
 *
 * Created on 26 de enero de 2009, 11:47
 */

package sistcomptf;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.ImageIcon;
/**
 *
 * @author  Medina
 */
public class StatusView extends javax.swing.JPanel implements Actualizable{
    private Actualizador hiloActualizador;
    private Thread thread;
    private String temp;
    private Integer itemp = new Integer (0);
    private ConeccionView coneccion;
    private Integer puertoCom;
    private Comunicador comManager;
    private Integer tempSet;
    /** Creates new form StatusView */
    public StatusView() {
        
        
        initComponents();
        
        
        
        hiloActualizador = new Actualizador(this);
        thread = new Thread(hiloActualizador);
      
                
        thread.start();
    }

  @Override
  protected void paintComponent(Graphics g)
  {
      Dimension tamanio = getSize();
      ImageIcon imagen = new ImageIcon(getClass().getResource("/sistcomptf/resources/Visual/bluedxs.jpg"));

      g.drawImage(imagen.getImage(),0,0,tamanio.width, tamanio.height,null);
      setOpaque(false);
      super.paintComponent(g); 
  }
    public StatusView(ConeccionView aThis, Comunicador  comManager) {
        this.setSize(getSize());
        
        initComponents();
        
        this.coneccion = aThis;
        this.comManager = comManager;
        
        hiloActualizador = new Actualizador(this);
        thread = new Thread(hiloActualizador);             
        
        puertoCom = Integer.parseInt(coneccion.getComNumber());
        statusCOM.setText(puertoCom.toString());
        
        thread.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        estadoLabel = new javax.swing.JLabel();
        coneccionLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        setBoton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        comLabel = new javax.swing.JLabel();
        tempActual = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        tempNueva = new javax.swing.JFormattedTextField();
        statusCOM = new javax.swing.JLabel();
        statusConeccion = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sistcomptf.SistCompTFApp.class).getContext().getResourceMap(StatusView.class);
        estadoLabel.setFont(resourceMap.getFont("statusConeccion.font")); // NOI18N
        estadoLabel.setText(resourceMap.getString("estadoLabel.text")); // NOI18N
        estadoLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        estadoLabel.setName("estadoLabel"); // NOI18N

        coneccionLabel.setFont(resourceMap.getFont("statusConeccion.font")); // NOI18N
        coneccionLabel.setForeground(resourceMap.getColor("coneccionLabel.foreground")); // NOI18N
        coneccionLabel.setText(resourceMap.getString("coneccionLabel.text")); // NOI18N
        coneccionLabel.setName("coneccionLabel"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        setBoton.setIcon(resourceMap.getIcon("setBoton.icon")); // NOI18N
        setBoton.setText(resourceMap.getString("setBoton.text")); // NOI18N
        setBoton.setBorder(null);
        setBoton.setBorderPainted(false);
        setBoton.setContentAreaFilled(false);
        setBoton.setName("setBoton"); // NOI18N
        setBoton.setPressedIcon(resourceMap.getIcon("setBoton.pressedIcon")); // NOI18N
        setBoton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                setBotonMouseClicked(evt);
            }
        });

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        comLabel.setFont(resourceMap.getFont("statusConeccion.font")); // NOI18N
        comLabel.setForeground(resourceMap.getColor("comLabel.foreground")); // NOI18N
        comLabel.setText(resourceMap.getString("comLabel.text")); // NOI18N
        comLabel.setName("comLabel"); // NOI18N

        tempActual.setFont(resourceMap.getFont("tempActual.font")); // NOI18N
        tempActual.setForeground(resourceMap.getColor("tempActual.foreground")); // NOI18N
        tempActual.setText(resourceMap.getString("tempActual.text")); // NOI18N
        tempActual.setName("tempActual"); // NOI18N

        jLabel9.setFont(resourceMap.getFont("jLabel9.font")); // NOI18N
        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        tempNueva.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        tempNueva.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#"))));
        tempNueva.setText(resourceMap.getString("tempNueva.text")); // NOI18N
        tempNueva.setDisabledTextColor(resourceMap.getColor("tempNueva.disabledTextColor")); // NOI18N
        tempNueva.setFont(resourceMap.getFont("tempNueva.font")); // NOI18N
        tempNueva.setMaximumSize(new java.awt.Dimension(120, 120));
        tempNueva.setName("tempNueva"); // NOI18N

        statusCOM.setFont(resourceMap.getFont("statusConeccion.font")); // NOI18N
        statusCOM.setForeground(resourceMap.getColor("statusCOM.foreground")); // NOI18N
        statusCOM.setText(resourceMap.getString("statusCOM.text")); // NOI18N
        statusCOM.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        statusCOM.setName("statusCOM"); // NOI18N

        statusConeccion.setFont(resourceMap.getFont("statusConeccion.font")); // NOI18N
        statusConeccion.setForeground(resourceMap.getColor("statusConeccion.foreground")); // NOI18N
        statusConeccion.setText(resourceMap.getString("statusConeccion.text")); // NOI18N
        statusConeccion.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        statusConeccion.setName("statusConeccion"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(setBoton, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
                        .addGap(269, 269, 269))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(estadoLabel, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tempNueva, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(comLabel)
                                    .addComponent(coneccionLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(statusCOM, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(statusConeccion, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 151, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(137, 137, 137)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(tempActual, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel9))
                                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)))))
                    .addComponent(jLabel5))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(47, 47, 47))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(tempActual, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                            .addComponent(tempNueva, javax.swing.GroupLayout.PREFERRED_SIZE, 87, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(setBoton)
                .addGap(39, 39, 39)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(comLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(statusCOM, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(coneccionLabel)
                            .addComponent(statusConeccion)))
                    .addComponent(estadoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33))
        );
    }// </editor-fold>//GEN-END:initComponents

private void setBotonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_setBotonMouseClicked
        boolean ACKstate = false;
// TODO add your handling code here:
    
    //Excepcion aca
    if(!tempNueva.getText().isEmpty()){
        setBoton.setEnabled(false);
        
        tempSet=Integer.parseInt(tempNueva.getText().toString());
        //Chequeo q la temperatura a enviar sea valida
        //(isTempvalid(temp2Send))? sendRs232C(temp):error(temp);
        if(comManager.validarTemp(tempSet)){
            
            if(comManager.sendDato(tempSet)!=0){
                statusConeccion.setText("Error en el envio de seteo de temperatura");
            }else{
                try {

                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    Logger.getLogger(StatusView.class.getName()).log(Level.SEVERE, null, ex);
                }
                ACKstate = comManager.getSetTempState();
                if(ACKstate){
                    //La temperatura fue correctamente seteada
                    statusConeccion.setText("La temperatura fue correctamente seteada "+ACKstate);
                }else{
                     statusConeccion.setText("La temperatura no fue seteada "+ACKstate);
                }                    
                }             
                             
        }
        else{
            //tratamiento de error
             statusConeccion.setText("El valor de temperatura ingresado excede los limites");
        }
        
     setBoton.setEnabled(true);        
    }
}//GEN-LAST:event_setBotonMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel comLabel;
    private javax.swing.JLabel coneccionLabel;
    private javax.swing.JLabel estadoLabel;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JButton setBoton;
    private javax.swing.JLabel statusCOM;
    private javax.swing.JLabel statusConeccion;
    private javax.swing.JLabel tempActual;
    private javax.swing.JFormattedTextField tempNueva;
    // End of variables declaration//GEN-END:variables

    public void actualizar(Integer tempNueva) {
        
        tempActual.setText(tempNueva.toString());
    }

}
